﻿Imports DAO
Imports BUS
Imports DTO
Imports DevComponents.DotNetBar

Public Class frmPhieuThuTien
    Public luusotienthu As Int64
    Public Shared makh As String

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnTimMaKH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimMaKH.Click
        Dim f As New frmDanhSachKhachHang()
        f.ShowDialog()
        hienthimakhachhang()
    End Sub

    Public Sub hienthimakhachhang()
        txtMaKH.Text = makh
    End Sub

    Private Sub frmPhieuThuTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ColCmbKH.DataSource = KhachHangDAO.GetKhachHangAll()
        ColCmbKH.ValueMember = "MaKhachHang"
        ColCmbKH.DisplayMember = "HoTenKhachHang"
        hienthiphieuthu()
    End Sub

    Public Sub hienthiphieuthu()
        dgvDanhSachPhieuThuTien.DataSource = PhieuThuDAO.GetPhieuThuAll()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim pt As New PhieuThuDTO()
        Try
            pt.MaKhachHang = Integer.Parse(txtMaKH.Text)
        Catch e1 As FormatException
            MessageBoxEx.Show("Mã khách hàng không được để trống")
            btnTimMaKH_Click(sender, e)
            Return
        End Try
        pt.NgayThu = dtNgayThuTien.Value

        Dim dt As DataTable = ThamSoDAO.GetThamSoAll()
        Dim ktchophep As Integer = Integer.Parse(dt.Rows(0).ItemArray(5).ToString())
        Dim dt2 As DataTable = ThamSoDAO.SlectTienNoKH(Integer.Parse(txtMaKH.Text))
        Dim tienno As Integer = Integer.Parse(dt2.Rows(0).ItemArray(5).ToString())
        Dim tienthu As Int64 = Int64.Parse(txtSoTienThu.Text)

        Try
            pt.SoTienThu = UInt64.Parse(txtSoTienThu.Text)
        Catch e2 As FormatException
            MessageBoxEx.Show("Số tiền thu không nhập đúng qui định")
            Return
        Catch e3 As OverflowException
            MessageBoxEx.Show("Số tiền nhập vào không được âm")
            Return
        End Try
        If ktchophep = 1 Then
            If tienno < tienthu Then
                MessageBoxEx.Show("Tiền thu đã lớn hơn tiền khách hàng đang nợ")
                Return
            End If
        End If
        Dim tiennonew As Int64 = tienno - tienthu

        If PhieuThuBUS.ThemPhieuThu(pt) = False Then
            MessageBoxEx.Show("Thêm thất bại", "Thông báo")
        Else
            Dim kh As New KhachHangDTO()
            kh.MaKhachHang = Integer.Parse(txtMaKH.Text)
            kh.TienNo = tiennonew
            KhachHangDAO.UpdateTienNo(kh)
            Dim cn As New BaoCaoCongNoDTO()
            cn.MaKhachHang = kh.MaKhachHang
            cn.NgayPhatSinh = pt.NgayThu
            cn.NoDau = tienno
            cn.NoCuoi = tiennonew
            cn.PhatSinh = "Phiếu thu tiền"
            BaoCaoCongNoDAO.Insert(cn)
            MessageBoxEx.Show("Thành công")
            hienthiphieuthu()
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim pt As New PhieuThuDTO()
        pt.MaPhieuThu = Integer.Parse(txtMaPhieuThu.Text)
        If PhieuThuBUS.XoaPhieuThu(pt) = False Then
            MessageBoxEx.Show("Xóa thất bại", "Thông báo")
        End If
        hienthiphieuthu()
    End Sub

    Private Sub dgvDanhSachPhieuThuTien_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachPhieuThuTien.SelectionChanged
        If dgvDanhSachPhieuThuTien.SelectedRows.Count > 0 Then
            txtMaPhieuThu.Text = dgvDanhSachPhieuThuTien.SelectedRows(0).Cells(0).Value.ToString()
            dtNgayThuTien.Text = dgvDanhSachPhieuThuTien.SelectedRows(0).Cells(1).Value.ToString()
            txtSoTienThu.Text = dgvDanhSachPhieuThuTien.SelectedRows(0).Cells(2).Value.ToString()
            luusotienthu = Int64.Parse(dgvDanhSachPhieuThuTien.SelectedRows(0).Cells(2).Value.ToString())
            txtMaKH.Text = dgvDanhSachPhieuThuTien.SelectedRows(0).Cells(3).Value.ToString()
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim pt As New PhieuThuDTO()
        pt.MaPhieuThu = Integer.Parse(txtMaPhieuThu.Text)
        pt.NgayThu = dtNgayThuTien.Value
        Try
            Dim kh As Integer = Integer.Parse(txtMaKH.Text)
        Catch e1 As FormatException
            MessageBoxEx.Show("Mã khách hàng không để trống")
            btnTimMaKH_Click(sender, e)
            Return
        End Try
        Dim dt As DataTable = ThamSoDAO.GetThamSoAll()
        Dim ktchophep As Integer = Integer.Parse(dt.Rows(0).ItemArray(5).ToString())
        Dim dt2 As DataTable = ThamSoDAO.SlectTienNoKH(Integer.Parse(txtMaKH.Text))
        Dim tienno As Integer = Integer.Parse(dt2.Rows(0).ItemArray(5).ToString())
        Dim tienthu As Int64 = Int64.Parse(txtSoTienThu.Text)


        Try
            pt.SoTienThu = UInt64.Parse(txtSoTienThu.Text)
        Catch e2 As FormatException
            MessageBoxEx.Show("Số tiền thu chưa nhập đúng qui định")
            Return
        Catch e3 As OverflowException
            MessageBoxEx.Show("Số tiền thu không được âm")
            Return
        End Try
        If ktchophep = 1 Then
            If tienno < tienthu Then
                MessageBoxEx.Show("Tiền thu đã lớn hơn tiền khách hàng đang nợ")
                Return
            End If
        End If
        Dim tienthunew As Int64 = tienthu - luusotienthu
        Dim tiennonew As Int64 = tienno - tienthunew


        If PhieuThuBUS.SuaPhieuThu(pt) = False Then
            MessageBoxEx.Show("Sửa thất bại", "Thông báo")
        Else
            Dim kh As New KhachHangDTO()
            kh.MaKhachHang = Integer.Parse(txtMaKH.Text)
            kh.TienNo = tiennonew
            KhachHangDAO.UpdateTienNo(kh)
            Dim cn As New BaoCaoCongNoDTO()
            cn.MaKhachHang = kh.MaKhachHang
            cn.NgayPhatSinh = pt.NgayThu
            cn.NoDau = tienno
            cn.NoCuoi = tiennonew
            cn.PhatSinh = "Cập nhật phiếu thu tiền"
            BaoCaoCongNoDAO.Insert(cn)
            MessageBoxEx.Show("Thành công")
            hienthiphieuthu()
        End If
    End Sub

    Private Sub btnXoaTrang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTrang.Click
        txtSoTienThu.Clear()
        txtMaKH.Clear()
    End Sub

    Private Sub btnInPhieuThuTien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInPhieuThuTien.Click
        Dim f As New frmInPhieuThuTien()
        Dim rpt As New CrystalReportPhieuThuTien()
        rpt.SetDataSource(PhieuThuDAO.GetPhieuThuCondition(Integer.Parse(txtMaPhieuThu.Text)))
        f.CrystalReportViewerPhieuThuTien.ReportSource = rpt
        f.Show()
    End Sub
End Class
